package com.mq.producer;
 
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.mq.domain.Person;
 
public class Producer {
     
    public void produceMessage(String queueName, Person p1){
         
        //Create ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
         
        try{
            //Create Connection using the factory
            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //Create Queue
            Destination destQueue = session.createQueue(queueName);
            //Create Producer
            MessageProducer producer = session.createProducer(destQueue);
            //Create Message
            ObjectMessage message = session.createObjectMessage(p1);
            //Send Message
            producer.send(message);
            //Close Producer
            producer.close();
            //Close Session
            session.close();
            //Close Connection
            connection.close();          
        }catch(JMSException e){         
            System.out.println("Problem with ActiveMQ");  
            e.printStackTrace();
        }  
    }
}